/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
//import com.itextpdf.tool.xml.XMLWorkerHelper;
import org.ipti.br.tag.entity.Work;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.logging.Level;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpServletResponse;
import org.ipti.br.tag.util.LoggerUtil;

/**
 *
 * @author luancorumba
 */
@Named(value = "createPDFMB")
@RequestScoped
public class CreatePDFMB extends AppBaseBean{
    private static final int DEFAULT_BUFFER_SIZE = 10240;
    
    private String text;

    /**
     * Creates a new instance of CreatePDFMB
     */
    public CreatePDFMB() {
    }
    
    private void create(String path){
        try {
//            HtmlCleaner cleaner = new HtmlCleaner();
//            CleanerProperties props = cleaner.getProperties();
//            TagNode node = cleaner.clean(this.text);
//            new PrettyXmlSerializer(props).writeToStream(node, System.out);
            Document document = new Document(PageSize.LETTER);
            PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(path));
            pdfWriter.open();
            document.open();
//            document.addAuthor("Luan Corumba");
//            document.addCreator("IPTI");
//            document.addSubject("Será que funfa?");
//            document.addCreationDate();
//            document.addTitle("Como eu sou foda");
            
            System.out.println(this.text);
            System.out.println(pdfWriter.getPageNumber());
            System.out.println(pdfWriter.isPageEmpty());
//            XMLWorkerHelper xMLWorkerHelper = XMLWorkerHelper.getInstance();
//            xMLWorkerHelper.parseXHtml(pdfWriter, document, new StringReader("<html><body>"+this.text+"</body></html>"));
            document.close();
            System.out.println("Terminou");
        }catch(DocumentException e){
            e.printStackTrace();
        }catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void generate(){
        FacesContext facesContext = getFacesContext();
        ExternalContext externalContext = facesContext.getExternalContext();
        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

        String path = "resources/pdf/Relatorio.pdf";
        path = path.replaceAll("//W", "");
        path = externalContext.getRealPath(path);
        this.create(path);

        File file = new File(path);
        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        try {
            input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
            response.reset();
            response.setHeader("Content-Type", "application/pdf");
            response.setHeader("Content-Length", String.valueOf(file.length()));
            response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
            output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            output.flush();
        } catch (Exception ex) {
            ex.printStackTrace();
            LoggerUtil.log("SimpleFrequencyBean:export", Level.SEVERE, "Não enviou", ex);
        } finally {
            try {
                output.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
                LoggerUtil.log("SimpleFrequencyBean:export", Level.SEVERE, "Não fechou", e);
            }
        }
        facesContext.responseComplete();
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}
